#include "airOptionsPanel.h"
#include "defines/airType.h"

#include <iostream>

AirOptionsPanel::AirOptionsPanel(QWidget *_parent) : QWidget(_parent){
    active = false;
    type = CONSTANT;
    directionX = 1.0;
    directionY = 1.0;
    directionZ = 0.0;
    magnitude = 30.0;
    period = 5;
    repeat = 3;
    viscosity = 0.0178;


    QCheckBox *activeCheckBox = new QCheckBox("Ativar");
    activeCheckBox->setChecked(active);
    connect(activeCheckBox, SIGNAL(toggled(bool)), this, SLOT(changeActive(bool)));


    //------------------------------------Tipo
    QLabel *typeLabel = new QLabel(tr("Tipo:"));
    typeLabel->setEnabled(active);
    connect(activeCheckBox, SIGNAL(toggled(bool)), typeLabel, SLOT(setEnabled(bool)));

    QComboBox *typeComboBox = new QComboBox;
    typeComboBox->addItem(tr("Constante"));
    typeComboBox->addItem(tr("Senoidal"));
    typeComboBox->setEnabled(active);
    connect(activeCheckBox, SIGNAL(toggled(bool)), typeComboBox, SLOT(setEnabled(bool)));
    connect(typeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeType(int)));

    QHBoxLayout *typeLayout = new QHBoxLayout;
    typeLayout->addWidget(typeLabel);
    typeLayout->addWidget(typeComboBox);


    //se o periodo for zero.. ele vai indefinidamente

    //------------------------------------Periodo
    periodLabel = new QLabel(QString::fromUtf8("Periodo:"));
    periodLabel->setEnabled(active);
    connect(activeCheckBox, SIGNAL(toggled(bool)), periodLabel, SLOT(setEnabled(bool)));

    periodDoubleSpinBox = new QDoubleSpinBox();
    periodDoubleSpinBox->setRange(0.0, 500.0);
    periodDoubleSpinBox->setSingleStep(5);
    periodDoubleSpinBox->setValue(period);
    periodDoubleSpinBox->setEnabled(active);
    connect(periodDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changePeriod(double)));
    connect(activeCheckBox, SIGNAL(toggled(bool)), this, SLOT(enablePeriodRepeat()));
    connect(typeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(enablePeriodRepeat()));

    QHBoxLayout *periodLayout = new QHBoxLayout;
    periodLayout->addWidget(periodLabel);
    periodLayout->addWidget(periodDoubleSpinBox);


    //------------------------------------Repetições
    repeatLabel = new QLabel(QString::fromUtf8("Repetições:"));
    repeatLabel->setEnabled(active);
    connect(activeCheckBox, SIGNAL(toggled(bool)), repeatLabel, SLOT(setEnabled(bool)));

    repeatDoubleSpinBox = new QDoubleSpinBox();
    repeatDoubleSpinBox->setRange(1.0, 500.0);
    repeatDoubleSpinBox->setSingleStep(5);
    repeatDoubleSpinBox->setValue(repeat);
    repeatDoubleSpinBox->setEnabled(active);
    connect(repeatDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRepeat(double)));
    connect(activeCheckBox, SIGNAL(toggled(bool)), this, SLOT(enablePeriodRepeat()));
    connect(typeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(enablePeriodRepeat()));

    QHBoxLayout *repeatLayout = new QHBoxLayout;
    repeatLayout->addWidget(repeatLabel);
    repeatLayout->addWidget(repeatDoubleSpinBox);


    //------------------------------------Viscosidade
    QLabel *viscosityLabel = new QLabel(QString::fromUtf8("Viscosidade:"));
    viscosityLabel->setEnabled(active);
    connect(activeCheckBox, SIGNAL(toggled(bool)), viscosityLabel, SLOT(setEnabled(bool)));

    QDoubleSpinBox *viscosityDoubleSpinBox = new QDoubleSpinBox();
    viscosityDoubleSpinBox->setRange(0.0, 50.0);
    viscosityDoubleSpinBox->setSingleStep(0.001);
    viscosityDoubleSpinBox->setDecimals(5);
    viscosityDoubleSpinBox->setValue(viscosity);
    viscosityDoubleSpinBox->setEnabled(active);
    connect(viscosityDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeViscosity(double)));
    connect(activeCheckBox, SIGNAL(toggled(bool)), viscosityDoubleSpinBox, SLOT(setEnabled(bool)));

    QHBoxLayout *viscosityLayout = new QHBoxLayout;
    viscosityLayout->addWidget(viscosityLabel);
    viscosityLayout->addWidget(viscosityDoubleSpinBox);


    //------------------------------------Magnitude
    QLabel *magnitudeLabel = new QLabel(QString::fromUtf8("Magnitude:"));
    magnitudeLabel->setEnabled(active);
    connect(activeCheckBox, SIGNAL(toggled(bool)), magnitudeLabel, SLOT(setEnabled(bool)));

    QDoubleSpinBox *magnitudeDoubleSpinBox = new QDoubleSpinBox();
    magnitudeDoubleSpinBox->setRange(0.0, 500.0);
    magnitudeDoubleSpinBox->setSingleStep(10);
    magnitudeDoubleSpinBox->setValue(magnitude);
    magnitudeDoubleSpinBox->setEnabled(active);
    connect(magnitudeDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMagnitude(double)));
    connect(activeCheckBox, SIGNAL(toggled(bool)), magnitudeDoubleSpinBox, SLOT(setEnabled(bool)));

    QHBoxLayout *magnitudeLayout = new QHBoxLayout;
    magnitudeLayout->addWidget(magnitudeLabel);
    magnitudeLayout->addWidget(magnitudeDoubleSpinBox);


    //--------------------------------------Direção
    QLabel *directionXLabel = new QLabel(tr("X:"));

    QDoubleSpinBox *directionXDoubleSpinBox = new QDoubleSpinBox();
    directionXDoubleSpinBox->setRange(-50.0, 50.0);
    directionXDoubleSpinBox->setSingleStep(1.0);
    directionXDoubleSpinBox->setValue(directionX);
    connect(directionXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeDirectionX(double)));


    QLabel *directionYLabel = new QLabel(tr("Y:"));

    QDoubleSpinBox *directionYDoubleSpinBox = new QDoubleSpinBox();
    directionYDoubleSpinBox->setRange(-50.0, 50.0);
    directionYDoubleSpinBox->setSingleStep(1.0);
    directionYDoubleSpinBox->setValue(directionY);
    connect(directionYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeDirectionY(double)));


    QLabel *directionZLabel = new QLabel(tr("Z:"));

    QDoubleSpinBox *directionZDoubleSpinBox = new QDoubleSpinBox();
    directionZDoubleSpinBox->setRange(-50.0, 50.0);
    directionZDoubleSpinBox->setSingleStep(1.0);
    directionZDoubleSpinBox->setValue(directionZ);
    connect(directionZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeDirectionZ(double)));


    QHBoxLayout *directionLayout = new QHBoxLayout;
    directionLayout->addWidget(directionXLabel);
    directionLayout->addWidget(directionXDoubleSpinBox);
    directionLayout->addStretch(1);
    directionLayout->addWidget(directionYLabel);
    directionLayout->addWidget(directionYDoubleSpinBox);
    directionLayout->addStretch(1);
    directionLayout->addWidget(directionZLabel);
    directionLayout->addWidget(directionZDoubleSpinBox);
    directionLayout->addStretch(1);

    QGroupBox *directionGroupBox = new QGroupBox(QString::fromUtf8("Direção"));
    directionGroupBox->setLayout(directionLayout);
    directionGroupBox->setEnabled(active);
    connect(activeCheckBox, SIGNAL(toggled(bool)), directionGroupBox, SLOT(setEnabled(bool)));



    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(activeCheckBox);
    mainLayout->addLayout(typeLayout);
    mainLayout->addLayout(periodLayout);
    mainLayout->addLayout(repeatLayout);
    mainLayout->addLayout(viscosityLayout);
    mainLayout->addLayout(magnitudeLayout);
    mainLayout->addWidget(directionGroupBox);
	mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void AirOptionsPanel::enablePeriodRepeat(){
    bool option = ( (type == SIN) && active );
    periodLabel->setEnabled(option);
    periodDoubleSpinBox->setEnabled(option);
    repeatLabel->setEnabled(option);
    repeatDoubleSpinBox->setEnabled(option);
}
